﻿import QtQuick 2.0

import "../components"
import "../camera"
import "../chromatogram"

Item {


    id: root

    property bool editable: true

    MouseArea{
        anchors.fill: parent
    }

    property bool showDetails: false        // true:详细版  false:简化版
    property bool showGraph: true
//    property int showModel: 0 // 0:优先显示动画 1:能谱图 2:摄像头
    property int showModel: 0    // 0:动画  1:摄像头  2: 能谱图         // 什么玩意呀……

    property real graphWidth: Math.min(width, height*1.5)*0.19 * 1.3
    property real graphHeight: Math.min(width, height*1.5)*0.19

    function startAnimation(){
        canvas.startAnimation()
    }

    property int syringeNum: 6
    property var syringeResetModel: ["全部复位", "注射器1复位", "注射器2复位", "注射器3复位", "注射器4复位", "注射器5复位", "注射器6复位"]

    onSyringeNumChanged: {
        syringeResetModel = []
        syringeResetModel.push("全部复位")
        for(let i = 1; i <= syringeNum; i ++){
            syringeResetModel.push("注射器" + i + "复位")
        }
        syringeResetModelChanged()
    }


    // 刷新耗材
    function refreshConsumable(consumableName){
        syringeNum = CExeConfig.getSyringeNumByConsumableName(consumableName)
    }

    signal fullScreen()

    NuclideCanvas{
        id: canvas
        controller: 0
        showDetails: root.showDetails && showModel===0
        visible: showModel !== 2
        width: showModel === 0 ? parent.width : graphWidth
        height: showModel == 0 ? parent.height : graphHeight
        x: showModel===0 ? 0 : root.width - width - 20*dp
        y: showModel===0 ? 0 : 20*dp
        z: showModel === 0 ? 0 : 1
    }

    CameraDisplay{
        // 摄像头显示
        id: cameraDisplay
        visible: showDetails || showModel === 1
        width: showModel === 1 ? parent.width : graphWidth
        height: showModel == 1 ? parent.height : graphHeight
        x: showModel===1 ? 0 : root.width - width - 20*dp
        y: showModel===1 ? 0 : 20*dp
        z: showModel === 0 ? 1 : 0
    }

    Chromatogram{
        id: chromatogram
        visible: (!root.showDetails) && showModel !==1

        width: showModel === 2 ? parent.width : graphWidth
        height: showModel === 2 ? parent.height : graphHeight
        x: showModel===2 ? 0 : root.width - width - 20*dp
        y: showModel===2 ? 0 : 20*dp
        z: showModel === 0 ? 1 : 0
        Text{
            text: "能谱图"
            x: 14*dp; y: 14*dp
            font.pixelSize: config.fontsize+2*dp
            font.bold: true
            visible: (!root.showDetails && showModel===0)
        }
    }



    MouseArea{      // 右上角的点击区域
        id: mouseArea_graph
        width: graphWidth; height: graphHeight
        x: root.width - width - 10*dp; y: 10*dp
        cursorShape: Qt.PointingHandCursor
        propagateComposedEvents: true
        hoverEnabled: true
        visible: !(!showDetails&&showModel===2)
        property bool isHovered: false
        onEntered: { isHovered = true }
        onExited: { isHovered = false }
        onDoubleClicked: {
            if(showDetails){
                showModel = Math.abs(showModel-1)
            }else{
                if(showModel === 0){
                    showModel = 2
                }else{
                    showModel = 0
                }
            }
        }onClicked: {
            mouse.accepted = false
        }onReleased: {
            mouse.accepted = false
        }
        z: 4
        CustomTooltip{
            visible: parent.isHovered
            text: showModel===0&&!showDetails ? "能谱图双击放大" : "双击放大显示"
        }
    }

    // 切换详细版和简化版
    Rectangle{
        height: width*2; width: 36*dp
        x: parent.width - width - 26*dp
        y: parent.height - height - (flagtab===0 ? 85*dp : 26*dp)
        radius: 2*dp
        Rectangle{              // 上面的
            height: parent.width; width: parent.width
            color: (flagtab === 0 || isHovered) ? "#1677FF" : "white"
            visible: parent
            property bool isHovered: false
            Image{
                source: (flagtab === 0 || parent.isHovered) ? "qrc:/picture/tabToSample_1.png" : "qrc:/picture/tabToSample_b.png"
                height: 20*dp; width: 20*dp
                anchors.centerIn: parent
            }MouseArea{
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                hoverEnabled: true
                onEntered: {
                    parent.isHovered = true
                }onExited: {
                    parent.isHovered = false
                }
                onClicked: {
                    flagtab = 0
                }
            }
            CustomTooltip{
                visible: parent.isHovered
                text: "切换详细版"
            }
            radius: 2*dp
        }Rectangle{
            y: parent.width
            height: parent.width; width: parent.width
            color: (flagtab===1 || isHovered) ? "#1677FF" : "white"
            visible: parent
            property bool isHovered: false
            Image{
                source: (flagtab===1 || parent.isHovered) ? "qrc:/picture/tabToDetails_1.png" : "qrc:/picture/tabToDetails_b.png"
                height: 20*dp; width: 20*dp
                anchors.centerIn: parent
            }MouseArea{
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                hoverEnabled: true
                onEntered: {
                    parent.isHovered = true
                }onExited: {
                    parent.isHovered = false
                }
                onClicked: {
                    flagtab = 1
                }
            }
            CustomTooltip{
                visible: parent.isHovered
                text: "切换简化版"
            }
            radius: 2*dp
        }
    }

    Text{
        x: 20*dp; y: 20*dp
        height: 20*dp; width: 120*dp
        font.pixelSize: config.fontsize + 5*dp
        text: showModel===0 ? "实验过程" : (showModel===1 ? "摄像头" : "能谱图")
        font.bold: true
        z: 3
    }
    PushButton{
        icon_color: "#1677FF"
        icon_height: 20*dp; icon_width: 20*dp
        button_icon: "qrc:/picture/back.png"
        height: 30*dp; width: 70*dp
        title: "返回"
        text_size: 17*dp
        button_color: "transparent"
        text_color: "#1677FF"
        onReleased: {
            showModel = 0
        }
        visible: showModel > 0
        x: 100*dp; y: 20*dp; z: 3
    }

    // 全屏
    Rectangle{
        id: button_fullScreen
        height: 36*dp; width: 36*dp
        radius: 2*dp
        visible: showDetails
        x: root.width - width - 26*dp; y: root.height - 60*dp
        z: 3
        color: isHovered ? "#1677FF" : "white"
        property bool isHovered: false
        Image{
            anchors.centerIn: parent
            source: parent.isHovered ? "qrc:/picture/tabToFullScreen_1.png" : "qrc:/picture/tabToFullScreen_b.png"
            height: 20*dp; width: 20*dp
        }
        MouseArea{
            anchors.fill: parent
            hoverEnabled: true
            cursorShape: Qt.PointingHandCursor
            onExited: { parent.isHovered = false }
            onEntered: { parent.isHovered = true }
            onClicked: {
                root.fullScreen()
            }
        }
        CustomTooltip{
            visible: parent.isHovered
            text: "全屏"
        }
    }


    Item{

        x: showDetails ? 20*dp : 155*dp; y: root.height - 40*dp - 20*dp
        visible: showModel === 0
        CustomComboButton{
            id: comboButton_syringe
            editable: root.editable
            title: "     注射器复位"
            x: 2*dp
            comboBgColor: "#1677FF"
            comboBgColor_hover: "#0958d9"
            comboFontColor: "white"
            height: 40*dp; width: 142*dp
            model: syringeResetModel
            onTextClicked: {
                if(text === "全部复位"){
                    CAnimation.resetPipe()
//                    for(let i = 0; i < syringeNum; i ++){
//                        CMaintenance.reset("1", (i+1).toString(), expConsumblesType.getText())
//                    }
                    CMaintenance.reset("1", "9", expConsumblesType.getText())
//                    CAnimation.changeSyringe(1, 1, 0.3)
//                    CAnimation.changeSyringe(2, 1, 0.3)
//                    CAnimation.changeSyringe(3, 0, 0.3)
//                    CAnimation.changeSyringe(4, 1, 0.3)
//                    CAnimation.changeSyringe(5, 1, 0.3)
//                    CAnimation.changeSyringe(6, 1, 0.3)
                    CAnimation.resetSyringe(1)
                    CAnimation.resetSyringe(2)
                    CAnimation.resetSyringe(3)
                    CAnimation.resetSyringe(4)
                    CAnimation.resetSyringe(5)
                    CAnimation.resetSyringe(6)
                }
                else{
                    let idx = text[3]
                    CAnimation.resetSyringe(idx)
                    CMaintenance.reset("1", idx.toString(), expConsumblesType.getText())
//                    CAnimation.setPipeSyringeSpeed(idx, 0.5)
                }
            }
        }
        CustomButton{
            x: 165*dp
            title: "阀门全开"
            enabled: root.editable
            defaultMode1: true
            button_icon: "qrc:/picture/valve_1.png"
            onClicked: {
                if(title === "阀门全开"){
                    title = "阀门全关"
                    CAnimation.changeValve(0,  true)
                    CAnimation.changeValve(1,  true)
                    CAnimation.changeValve(2,  true)
                    CAnimation.changeValve(3,  true)
                    CAnimation.changeValve(4,  true)
                    CAnimation.changeValve(5,  true)
                    CAnimation.changeValve(6,  true)
                    CAnimation.changeValve(7,  true)
                    CAnimation.changeValve(8,  true)
                    CAnimation.changeValve(9,  true)
                    CAnimation.changeValve(10, true)
                    CAnimation.changeValve(11, true)
                    CAnimation.changeValve(12, true)
                    CAnimation.changeValve(13, true)
                    CAnimation.changeValve(14, true)
                    CommunicationMethod.stepOperatingSimulation("2", ["开启","1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24"])
                }else{
                    title = "阀门全开"
                    CAnimation.changeValve(0,  false)
                    CAnimation.changeValve(1,  false)
                    CAnimation.changeValve(2,  false)
                    CAnimation.changeValve(3,  false)
                    CAnimation.changeValve(4,  false)
                    CAnimation.changeValve(5,  false)
                    CAnimation.changeValve(6,  false)
                    CAnimation.changeValve(7,  false)
                    CAnimation.changeValve(8,  false)
                    CAnimation.changeValve(9,  false)
                    CAnimation.changeValve(10, false)
                    CAnimation.changeValve(11, false)
                    CAnimation.changeValve(12, false)
                    CAnimation.changeValve(13, false)
                    CAnimation.changeValve(14, false)
                    CommunicationMethod.stepOperatingSimulation("2", ["关闭","1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24"])
                }
            }
        }
        CustomComboButton{
            id: comboButton_wheel
            x: 300*dp
            editable: root.editable
            title: "      转盘转动"
            comboBgColor: "#1677FF"
            comboFontColor: "white"
            height: 40*dp; width: 135*dp
            comboBgColor_hover: "#0958d9"
            model: ["复位", "位置1", "位置2", "位置3", "位置4", "位置5", "位置6", "位置7", "位置8", "位置9"]
            onTextClicked: {
                if(text === "复位"){
                    CMaintenance.reset("1", "7", expConsumblesType.getText())
                    CAnimation.resetWheel(expConsumblesType.getText())
                }else{
                    let index = Number(text[2])
                    CommunicationMethod.stepOperatingSimulation("3", [index.toString()])
                    CAnimation.turnWheel_posi(index-1, 1)
                }
            }
        }
        Image{      // 我是转盘的图标
            x: comboButton_wheel.x + 12*dp; y: 10*dp
            height: 20*dp; width: 20*dp
            source: "qrc:/picture/button_wheel.png"
        }
        Image{      // 我是注射器的图标
            x: comboButton_syringe.x + 12*dp; y: 10*dp
            height: 19*dp; width: 19*dp
            source: "qrc:/picture/button_syringe.png"
        }
        // 摄像头
        Image{
            id: button_camera
            height: 32*dp; width: 32*dp
            visible: !showDetails       // 在简化版显示
            x: 490*dp
            source: "qrc:/picture/camera.png"
            MouseArea{
                cursorShape: Qt.PointingHandCursor
                anchors.fill: parent
                onClicked: {
                    showModel = 1
                }
            }
        }
    }


}
